import { defineStore } from "pinia";

export default defineStore("user", {
  state() {
    const storedUserInfo = localStorage.getItem('userInfo');
    return {
      token: localStorage.getItem('token') || '',
      userInfo: storedUserInfo ? JSON.parse(storedUserInfo) : null,
    };
  },
  getters: {
    getToken: (state) => state.token,
    getUserInfo: (state) => state.userInfo,
  },
  actions: {
    removeUser() {
      this.removeToken();
      this.removeUserInfo();
    },
    setUserInfo(userInfo: any) {
      this.userInfo = userInfo;
      localStorage.setItem('userInfo', JSON.stringify(userInfo));
    },
    removeUserInfo() {
      this.userInfo = null;
      localStorage.removeItem('userInfo');
    },
    setToken(token: string) {
      this.token = token;
      localStorage.setItem('token', token);
    },
    removeToken() {
      this.token = '';
      localStorage.removeItem('token');
    },
  },
});